"""日志系统模块"""
import logging
from colorama import Fore, Style, init

# 初始化colorama以支持Windows
init(autoreset=True)

class ColoredFormatter(logging.Formatter):
    """带颜色的日志格式化器"""
    COLORS = {
        'DEBUG': Fore.CYAN,
        'INFO': Fore.GREEN,
        'WARNING': Fore.YELLOW,
        'ERROR': Fore.RED,
        'CRITICAL': Fore.RED + Style.BRIGHT
    }
    
    def format(self, record):
        color = self.COLORS.get(record.levelname, '')
        if color:
            record.name = color + record.name + Style.RESET_ALL
            record.levelname = color + record.levelname + Style.RESET_ALL
            record.msg = color + record.msg + Style.RESET_ALL
        return super().format(record)

def get_logger(name: str = "py2so", verbose: bool = False) -> logging.Logger:
    """获取配置好的日志器"""
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG if verbose else logging.INFO)
    
    # 避免重复添加处理器
    if logger.handlers:
        return logger
    
    handler = logging.StreamHandler()
    formatter = ColoredFormatter('%(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    
    logger.addHandler(handler)
    return logger
